<?php
class Widget_model extends CI_Model
{
    public function __construct()
    {
        $this->load->database();
    }

    public function get_widget()
    {
        $query = $this->db->get("t_widgets");
        return $query->result_array();
    }

    public function get_widget_byname($name)
    {
        $query = $this->db->get_where('t_widgets',array('name' => $name));
        return $query->result_array();
    }

    public function get_widget_bytype($type)
    {
        $query = $this->db->get_where('t_widgets',array('type' => $type));
        return $query->result_array();
    }

    public function set_widget($type)
    {
        $data = array(
            'name' => $this->input->post('widget'),
            'type' => $type
        );

        return $this->db->insert('t_widgets',$data);
    }


    public function delete_widget($name)
    {
        $widget = $this->get_widget_byname($name);
        if($widget[0]['type'] == 'blog')
        {
            $this->load->model("blog_model");
            $this->blog_model->delete_blog_bywidget($name);
        }
        elseif($widget[0]['type'] == 'text')
        {
            $this->load->model("text_model");
            $this->text_model->delete_text_bywidget($name);
        }
        elseif($widget[0]['type'] == 'textlink')
        {
            $this->load->model("textlink_model");
            $this->textlink_model->delete_textlink_bywidget($name);
        }
        $this->db->delete('t_widgets',array('name' => $name));
    }
}
?>
